package com.yehuishou.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.SellItemDao;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.Configurations;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.Item;
import com.yehuishou.pojo.ItemClassify;
import com.yehuishou.pojo.Models;
import com.yehuishou.pojo.SelItemParameter;
import com.yehuishou.service.SellItemService;

@Service
public class SellItemServiceImpl implements SellItemService {
	@Resource
	private SellItemDao sellitemdao;

	@Override
	public PageInfo<ItemClassify> itemclassifysel(Integer pageNum, Integer pageSize, SelItemParameter sip) {
		PageHelper.startPage(pageNum, pageSize);
		// TODO Auto-generated method stub
		List<ItemClassify> list = sellitemdao.itemclassifysel(sip);
		for (int i = 0; i < list.size(); i++) {
			String simg = list.get(i).getImage();
			if (simg != null && !"".equals(simg)) {
				String[] img = (simg.substring(1, simg.length())).split(";");
				Integer s = Integer.parseInt(img[0]);
				List<ImageFile> imagefile = sellitemdao.selimg(s);
				String imgpath = imagefile.get(0).getFilePath();
				list.get(i).setImage(imgpath);
			}
		}
		PageInfo<ItemClassify> pageinfo = new PageInfo<ItemClassify>(list);
		return pageinfo;
	}

	@Override
	public int itemdele(Integer i_id) {
		// TODO Auto-generated method stub
		int i = sellitemdao.itemdele(i_id);
		Configurations conf=new Configurations();
		conf.setI_id(i_id);
		sellitemdao.confdele(conf);
		return i;
	}

	@Override
	public List<Classify> selclassify() {
		// TODO Auto-generated method stub
		List<Classify> list = sellitemdao.selclassify();
		return list;
	}

	@Override
	public List<Brand> selbrand(Integer cid) {
		// TODO Auto-generated method stub
		List<Brand> brand = sellitemdao.selbrand(cid);
		return brand;
	}

	@Override
	public List<Models> selmodel(Integer pid) {
		// TODO Auto-generated method stub
		List<Models> model = sellitemdao.selmodel(pid);
		return model;
	}

	@Override
	public int itemadd(Item item) {
		// TODO Auto-generated method stub
		int i = sellitemdao.itemadd(item);
		return i;
	}

	@Override
	public int configurationsadd(Configurations con) {
		// TODO Auto-generated method stub
		int c=sellitemdao.configurationsadd(con);
		return c;
	}
	
	@Override
	public Map<String, Object> itemsel(Integer i_id) {
		// TODO Auto-generated method stub
		List<Item> item = sellitemdao.itemsel(i_id);
		Map<String, Object> map = new HashMap<>();
		//获取商品全部图片
		List<ImageFile> imgf = new ArrayList<ImageFile>();
		String imgs = item.get(0).getImage();
		if (imgs != null && !"".equals(imgs)) {
			String[] img = imgs.substring(1, imgs.length()).split(";");
			for (int i = 0; i < img.length; i++) {
				Integer s = Integer.parseInt(img[i]);
				List<ImageFile> imagefile = sellitemdao.selimg(s);
				imgf.add(imagefile.get(0));

			}
		}
		//获取商品底部详情全部图片
		List<ImageFile> imgfparticulars = new ArrayList<ImageFile>();
		String imgsparticulars = item.get(0).getImageparticulars();
		if (imgsparticulars != null && !"".equals(imgsparticulars)) {
			String[] imgparticulars = imgsparticulars.substring(1, imgsparticulars.length()).split(";");
			for (int i = 0; i < imgparticulars.length; i++) {
				Integer s = Integer.parseInt(imgparticulars[i]);
				List<ImageFile> imagefile = sellitemdao.selimg(s);
				imgfparticulars.add(imagefile.get(0));
			}
		}
		List<Configurations> conf=sellitemdao.selconfiguration(i_id);
		map.put("item", item);
		map.put("imgf", imgf);
		map.put("imgfparticulars", imgfparticulars);
		map.put("conf", conf);
		return map;
	}

	@Override
	public List<Brand> selbrand() {
		// TODO Auto-generated method stub
		List<Brand> brand = sellitemdao.selbrand();
		return brand;
	}

	@Override
	public List<Models> selmodel() {
		// TODO Auto-generated method stub
		List<Models> model = sellitemdao.selmodel();
		return model;
	}

	@Override
	public int save(ImageFile imgfile) {
		// TODO Auto-generated method stub
		int i = sellitemdao.save(imgfile);
		return i;
	}

	@Override
	public int itemupdate(Item item) {
		// TODO Auto-generated method stub
		int i = sellitemdao.itemupdate(item);
		return i;
	}
	
	@Override
	public int configurationsupdate(Configurations conf) {
		// TODO Auto-generated method stub
		int i=sellitemdao.configurationsupdate(conf);
		return i;
	}

	@Override
	public int checktitle(String title,Integer cid,Integer pid,Integer mid) {
		// TODO Auto-generated method stub
		List<Item> list=sellitemdao.checktitle(title,cid,pid,mid);
		if(list.size()>0){
			return 1;
		}
		return 0;
	}

	@Override
	public int confdele(Configurations conf) {
		// TODO Auto-generated method stub
		int i=sellitemdao.confdele(conf);
		return i;
	}

	@Override
	public int imageparticularsadd(Item item) {
		// TODO Auto-generated method stub
		int i=sellitemdao.imageparticularsadd(item);
		return i;
	}

	@Override
	public int imageparticularsupdate(Item item) {
		// TODO Auto-generated method stub
		int i=sellitemdao.imageparticularsupdate(item);
		return i;
	}


}
